﻿Public Class TMyGlobalPrivileges
    Implements System.IDisposable

    Private _PrivList As New List(Of Boolean)
    Friend _Password As String
    Friend _SelectPriv As Boolean
    Friend _InsertPriv As Boolean
    Friend _UpdatePriv As Boolean
    Friend _DeletePriv As Boolean
    Friend _CreatePriv As Boolean
    Friend _DropPriv As Boolean
    Friend _ReloadPriv As Boolean
    Friend _ShutdownPriv As Boolean
    Friend _ProcessPriv As Boolean
    Friend _FilePriv As Boolean
    Friend _GrantPriv As Boolean
    Friend _ReferencesPriv As Boolean
    Friend _IndexPriv As Boolean
    Friend _AlterPriv As Boolean
    Friend _ShowDbPriv As Boolean
    Friend _SuperPriv As Boolean
    Friend _CreateTmpTablePriv As Boolean
    Friend _LockTablesPriv As Boolean
    Friend _ExecutePriv As Boolean
    Friend _ReplSlavePriv As Boolean
    Friend _ReplClientPriv As Boolean
    Friend _CreateViewPriv As Boolean
    Friend _ShowViewPriv As Boolean
    Friend _CreateRoutinePriv As Boolean
    Friend _AlterRoutinePriv As Boolean
    Friend _CreateUserPriv As Boolean
    Friend _EventPriv As Boolean
    Friend _TriggerPriv As Boolean
    Friend _CreateTablespacePriv As Boolean

    Friend _MaxQuestions As Integer
    Friend _MaxUpdates As Integer
    Friend _MaxConnections As Integer
    Friend _MaxUserConnections As Integer
    Friend _Plugin As String
    Friend _AuthenticationString As String
    Private _Owner As TMyUser
    Public Sub New(ByVal AOwner As TMyUser)
        _Owner = AOwner
        InitList()
    End Sub
    Public ReadOnly Property UserName() As String
        Get
            Return _Owner.UserName
        End Get
    End Property
    Public ReadOnly Property HostName() As String
        Get
            Return _Owner.Host
        End Get
    End Property
    Public ReadOnly Property Password() As String
        Get
            Return _Password
        End Get
    End Property
    Private ReadOnly Property Query() As TMyQuery
        Get
            Return _Owner.Query
        End Get
    End Property
    Public Sub Clear()
        _Password = String.Empty
        _SelectPriv = False
        _InsertPriv = False
        _UpdatePriv = False
        _DeletePriv = False
        _CreatePriv = False
        _DropPriv = False
        _ReloadPriv = False
        _ShutdownPriv = False
        _ProcessPriv = False
        _FilePriv = False
        _GrantPriv = False
        _ReferencesPriv = False
        _IndexPriv = False
        _AlterPriv = False
        _ShowDbPriv = False
        _SuperPriv = False
        _CreateTmpTablePriv = False
        _LockTablesPriv = False
        _ExecutePriv = False
        _ReplSlavePriv = False
        _ReplClientPriv = False
        _CreateViewPriv = False
        _ShowViewPriv = False
        _CreateRoutinePriv = False
        _AlterRoutinePriv = False
        _CreateUserPriv = False
        _EventPriv = False
        _TriggerPriv = False
        _CreateTablespacePriv = False

        _MaxQuestions = 0
        _MaxUpdates = 0
        _MaxConnections = 0
        _MaxUserConnections = 0
        _Plugin = String.Empty
        _AuthenticationString = String.Empty
    End Sub

    Public Sub Refresh()
        Me.Clear()
        Query.Clear()
        Query.SQL.AppendLine("select")
        Query.SQL.AppendLine(" Host")
        Query.SQL.AppendLine(",User")
        Query.SQL.AppendLine(",Password")
        Query.SQL.AppendLine(",Select_priv")
        Query.SQL.AppendLine(",Insert_priv")
        Query.SQL.AppendLine(",Update_priv")
        Query.SQL.AppendLine(",Delete_priv")
        Query.SQL.AppendLine(",Create_priv")
        Query.SQL.AppendLine(",Drop_priv")
        Query.SQL.AppendLine(",Reload_priv")
        Query.SQL.AppendLine(",Shutdown_priv")
        Query.SQL.AppendLine(",Process_priv")
        Query.SQL.AppendLine(",File_priv")
        Query.SQL.AppendLine(",Grant_priv")
        Query.SQL.AppendLine(",References_priv")
        Query.SQL.AppendLine(",Index_priv")
        Query.SQL.AppendLine(",Alter_priv")
        Query.SQL.AppendLine(",Show_db_priv")
        Query.SQL.AppendLine(",Super_priv")
        Query.SQL.AppendLine(",Create_tmp_table_priv")
        Query.SQL.AppendLine(",Lock_tables_priv")
        Query.SQL.AppendLine(",Execute_priv")
        Query.SQL.AppendLine(",Repl_slave_priv")
        Query.SQL.AppendLine(",Repl_client_priv")
        Query.SQL.AppendLine(",Create_view_priv")
        Query.SQL.AppendLine(",Show_view_priv")
        Query.SQL.AppendLine(",Create_routine_priv")
        Query.SQL.AppendLine(",Alter_routine_priv")
        Query.SQL.AppendLine(",Create_user_priv")
        Query.SQL.AppendLine(",Event_priv")
        Query.SQL.AppendLine(",Trigger_priv")
        Query.SQL.AppendLine(",Create_tablespace_priv")
        Query.SQL.AppendLine(",max_questions")
        Query.SQL.AppendLine(",max_updates")
        Query.SQL.AppendLine(",max_connections")
        Query.SQL.AppendLine(",max_user_connections")
        Query.SQL.AppendLine(",plugin")
        Query.SQL.AppendLine(",authentication_string")
        Query.SQL.AppendLine("from")
        Query.SQL.AppendLine(" mysql.user")
        Query.SQL.AppendLine("where")
        Query.SQL.AppendLine("host=?host")
        Query.SQL.AppendLine("and user=?user")
        Query.DeclareAndSet("host", _Owner.Host)
        Query.DeclareAndSet("user", _Owner.UserName)
        Query.Execute()
        If Not Query.Eof Then
            _Password = Query.FieldAsString("Password")
            _SelectPriv = Query.FieldAsString("Select_priv").Equals("Y")
            _InsertPriv = Query.FieldAsString("Insert_priv").Equals("Y")
            _UpdatePriv = Query.FieldAsString("Update_priv").Equals("Y")
            _DeletePriv = Query.FieldAsString("Delete_priv").Equals("Y")
            _CreatePriv = Query.FieldAsString("Create_priv").Equals("Y")
            _DropPriv = Query.FieldAsString("Drop_priv").Equals("Y")
            _ReloadPriv = Query.FieldAsString("Reload_priv").Equals("Y")
            _ShutdownPriv = Query.FieldAsString("Shutdown_priv").Equals("Y")
            _ProcessPriv = Query.FieldAsString("Process_priv").Equals("Y")
            _FilePriv = Query.FieldAsString("File_priv").Equals("Y")
            _GrantPriv = Query.FieldAsString("Grant_priv").Equals("Y")
            _ReferencesPriv = Query.FieldAsString("References_priv").Equals("Y")
            _IndexPriv = Query.FieldAsString("Index_priv").Equals("Y")
            _AlterPriv = Query.FieldAsString("Alter_priv").Equals("Y")
            _ShowDbPriv = Query.FieldAsString("Show_db_priv").Equals("Y")
            _SuperPriv = Query.FieldAsString("Super_priv").Equals("Y")
            _CreateTmpTablePriv = Query.FieldAsString("Create_tmp_table_priv").Equals("Y")
            _LockTablesPriv = Query.FieldAsString("Lock_tables_priv").Equals("Y")
            _ExecutePriv = Query.FieldAsString("Execute_priv").Equals("Y")
            _ReplSlavePriv = Query.FieldAsString("Repl_slave_priv").Equals("Y")
            _ReplClientPriv = Query.FieldAsString("Repl_client_priv").Equals("Y")
            _CreateViewPriv = Query.FieldAsString("Create_view_priv").Equals("Y")
            _ShowViewPriv = Query.FieldAsString("Show_view_priv").Equals("Y")
            _CreateRoutinePriv = Query.FieldAsString("Create_routine_priv").Equals("Y")
            _AlterRoutinePriv = Query.FieldAsString("Alter_routine_priv").Equals("Y")
            _CreateUserPriv = Query.FieldAsString("Create_user_priv").Equals("Y")
            _EventPriv = Query.FieldAsString("Event_priv").Equals("Y")
            _TriggerPriv = Query.FieldAsString("Trigger_priv").Equals("Y")
            _CreateTablespacePriv = Query.FieldAsString("Create_tablespace_priv").Equals("Y")
            _MaxQuestions = Query.FieldAsString("max_questions")
            _MaxUpdates = Query.FieldAsString("max_updates")
            _MaxConnections = Query.FieldAsString("max_connections")
            _MaxUserConnections = Query.FieldAsString("max_user_connections")
            _Plugin = Query.FieldAsString("plugin")
            _AuthenticationString = Query.FieldAsString("authentication_string")
        End If
        Query.Close()

    End Sub
    Public ReadOnly Property Privs(ByVal Index As Integer) As Boolean
        Get
            Return _PrivList(Index)
        End Get
    End Property
    Public ReadOnly Property PrivCount() As Integer
        Get
            Return _PrivList.Count
        End Get
    End Property
    Public ReadOnly Property PrivName(ByVal Index As Integer) As String
        Get
            Select Case Index
                Case 0
                    Return "Select"
                Case 1
                    Return "Insert"
                Case 2
                    Return "Update"
                Case 3
                    Return "Delete"
                Case 4
                    Return "Create"
                Case 5
                    Return "Drop"
                Case 6
                    Return "Reload"
                Case 7
                    Return "Shutdown"
                Case 8
                    Return "Process"
                Case 9
                    Return "File"
                Case 10
                    Return "Grant"
                Case 11
                    Return "References"
                Case 12
                    Return "Index"
                Case 13
                    Return "Alter"
                Case 14
                    Return "ShowDb"
                Case 15
                    Return "Super"
                Case 16
                    Return "Create Tmp Table"
                Case 17
                    Return "Lock Tables"
                Case 18
                    Return "Execute"
                Case 19
                    Return "Repl Slave"
                Case 20
                    Return "Repl Client"
                Case 21
                    Return "Create View"
                Case 22
                    Return "Show View"
                Case 23
                    Return "Create Routine"
                Case 24
                    Return "Alter Routine"
                Case 25
                    Return "Create User"
                Case 26
                    Return "Event"
                Case 27
                    Return "Trigger"
                Case 28
                    Return "Create Tablespace"
                Case Else
                    Throw New IndexOutOfRangeException
            End Select
        End Get
    End Property
    Private Sub InitList()
        _PrivList.Clear()
        _PrivList.Add(_SelectPriv)
        _PrivList.Add(_InsertPriv)
        _PrivList.Add(_UpdatePriv)
        _PrivList.Add(_DeletePriv)
        _PrivList.Add(_CreatePriv)
        _PrivList.Add(_DropPriv)
        _PrivList.Add(_ReloadPriv)
        _PrivList.Add(_ShutdownPriv)
        _PrivList.Add(_ProcessPriv)
        _PrivList.Add(_FilePriv)
        _PrivList.Add(_GrantPriv)
        _PrivList.Add(_ReferencesPriv)
        _PrivList.Add(_IndexPriv)
        _PrivList.Add(_AlterPriv)
        _PrivList.Add(_ShowDbPriv)
        _PrivList.Add(_SuperPriv)
        _PrivList.Add(_CreateTmpTablePriv)
        _PrivList.Add(_LockTablesPriv)
        _PrivList.Add(_ExecutePriv)
        _PrivList.Add(_ReplSlavePriv)
        _PrivList.Add(_ReplClientPriv)
        _PrivList.Add(_CreateViewPriv)
        _PrivList.Add(_ShowViewPriv)
        _PrivList.Add(_CreateRoutinePriv)
        _PrivList.Add(_AlterRoutinePriv)
        _PrivList.Add(_CreateUserPriv)
        _PrivList.Add(_EventPriv)
        _PrivList.Add(_TriggerPriv)
        _PrivList.Add(_CreateTablespacePriv)
    End Sub

    Public ReadOnly Property HasPassword() As Boolean
        Get
            Return _Password.Length > 0
        End Get
    End Property

    Public ReadOnly Property SelectPriv() As Boolean
        Get
            Return _SelectPriv
        End Get
    End Property

    Public ReadOnly Property InsertPriv() As Boolean
        Get
            Return _InsertPriv
        End Get
    End Property

    Public ReadOnly Property UpdatePriv() As Boolean
        Get
            Return _UpdatePriv
        End Get
    End Property

    Public ReadOnly Property DeletePriv() As Boolean
        Get
            Return _DeletePriv
        End Get
    End Property

    Public ReadOnly Property CreatePriv() As Boolean
        Get
            Return _CreatePriv
        End Get
    End Property

    Public ReadOnly Property DropPriv() As Boolean
        Get
            Return _DropPriv
        End Get
    End Property

    Public ReadOnly Property ReloadPriv() As Boolean
        Get
            Return _ReloadPriv
        End Get
    End Property

    Public ReadOnly Property ShutdownPriv() As Boolean
        Get
            Return _ShutdownPriv
        End Get
    End Property

    Public ReadOnly Property ProcessPriv() As Boolean
        Get
            Return _ProcessPriv
        End Get
    End Property

    Public ReadOnly Property FilePriv() As Boolean
        Get
            Return _FilePriv
        End Get
    End Property

    Public ReadOnly Property GrantPriv() As Boolean
        Get
            Return _GrantPriv
        End Get
    End Property

    Public ReadOnly Property ReferencesPriv() As Boolean
        Get
            Return _ReferencesPriv
        End Get
    End Property

    Public ReadOnly Property IndexPriv() As Boolean
        Get
            Return _IndexPriv
        End Get
    End Property

    Public ReadOnly Property AlterPriv() As Boolean
        Get
            Return _AlterPriv
        End Get
    End Property

    Public ReadOnly Property ShowDbPriv() As Boolean
        Get
            Return _ShowDbPriv
        End Get
    End Property

    Public ReadOnly Property SuperPriv() As Boolean
        Get
            Return _SuperPriv
        End Get
    End Property

    Public ReadOnly Property CreateTmpTablePriv() As Boolean
        Get
            Return _CreateTmpTablePriv
        End Get
    End Property

    Public ReadOnly Property LockTablesPriv() As Boolean
        Get
            Return _LockTablesPriv
        End Get
    End Property

    Public ReadOnly Property ExecutePriv() As Boolean
        Get
            Return _ExecutePriv
        End Get
    End Property

    Public ReadOnly Property ReplSlavePriv() As Boolean
        Get
            Return _ReplSlavePriv
        End Get
    End Property

    Public ReadOnly Property ReplClientPriv() As Boolean
        Get
            Return _ReplClientPriv
        End Get
    End Property

    Public ReadOnly Property CreateViewPriv() As Boolean
        Get
            Return _CreateViewPriv
        End Get
    End Property

    Public ReadOnly Property ShowViewPriv() As Boolean
        Get
            Return _ShowViewPriv
        End Get
    End Property

    Public ReadOnly Property CreateRoutinePriv() As Boolean
        Get
            Return _CreateRoutinePriv
        End Get
    End Property

    Public ReadOnly Property AlterRoutinePriv() As Boolean
        Get
            Return _AlterRoutinePriv
        End Get
    End Property

    Public ReadOnly Property CreateUserPriv() As Boolean
        Get
            Return _CreateUserPriv
        End Get
    End Property

    Public ReadOnly Property EventPriv() As Boolean
        Get
            Return _EventPriv
        End Get
    End Property

    Public ReadOnly Property TriggerPriv() As Boolean
        Get
            Return _TriggerPriv
        End Get
    End Property

    Public ReadOnly Property CreateTableSpacePriv() As Boolean
        Get
            Return _CreateTablespacePriv
        End Get
    End Property

    Public ReadOnly Property MaxQuestions() As Integer
        Get
            Return _MaxQuestions
        End Get
    End Property
    Public ReadOnly Property MaxUpdates() As Integer
        Get
            Return _MaxUpdates
        End Get
    End Property
    Public ReadOnly Property MaxConnections() As Integer
        Get
            Return _MaxConnections
        End Get
    End Property
    Public ReadOnly Property MaxUserConnections() As Integer
        Get
            Return _MaxUserConnections
        End Get
    End Property
    Public ReadOnly Property Plugin() As String
        Get
            Return _Plugin
        End Get
    End Property
    Public ReadOnly Property AuthenticationString() As String
        Get
            Return _AuthenticationString
        End Get
    End Property

    Private disposedValue As Boolean = False        ' 重複する呼び出しを検出するには

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: 他の状態を解放します (マネージ オブジェクト)。
            End If
            _PrivList.Clear()
            ' TODO: ユーザー独自の状態を解放します (アンマネージ オブジェクト)。
            ' TODO: 大きなフィールドを null に設定します。
        End If
        Me.disposedValue = True
    End Sub

#Region " IDisposable Support "
    ' このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
    Public Sub Dispose() Implements IDisposable.Dispose
        ' このコードを変更しないでください。クリーンアップ コードを上の Dispose(ByVal disposing As Boolean) に記述します。
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class
